package com.bskj.kuaifu.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.bskj.common.utils.EncryptUtil;
import com.bskj.common.utils.JSONUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bskj.common.bean.model.UserCar;
import com.bskj.common.bean.model.UserInfo;
import com.bskj.common.utils.LogUtils;
import com.bskj.common.utils.ResultMapUtil;
import com.bskj.common.utils.StringUtils;
import com.bskj.common.web.BaseController;
import com.bskj.kuaifu.bean.vo.req.UserReqVo;
import com.bskj.kuaifu.service.IUserInfoService;

/**
 * Created by mocaijin on 19/12/2017.
 */
@Controller
@RequestMapping("userCar")
public class UserCarController extends BaseController {
    @Autowired
    private IUserInfoService userInfoService;

    @RequestMapping("myCarNo")
    public ModelAndView getMyCarNo(Integer userId) throws Exception {
        ModelAndView mv = getRequestModelAndView();
        List<UserCar> userCars = userInfoService.getMyCarNo(userId);
        mv.addObject("userCars", userCars);
        mv.setViewName("/user/myCarNo");
        addMyCarNoParams(userId, mv);
        return mv;
    }
    
    @RequestMapping("addCarNo")
    public ModelAndView addCarNo(Integer userId, String jsonWechatUser, String userCouponEncryptStr) throws Exception {
    	JSONObject jsonUser=JSONUtil.getJSONFromString(jsonWechatUser);
    	ModelAndView mv = getRequestModelAndView();
    	
    	JSONObject generateCouponParams = new JSONObject();
        generateCouponParams.put("uri", "/userCoupon/page/receiveCoupon");
        generateCouponParams.put("userId", userId);
        generateCouponParams.put("subscribe",jsonUser.get("subscribe"));
        generateCouponParams.put("jsonWechatUser",jsonWechatUser);
        LogUtils.debug(jsonWechatUser+jsonUser.get("subscribe"));
        generateCouponParams.put("userCouponEncryptStr", userCouponEncryptStr);
        
        mv.addObject("generateCouponParams", EncryptUtil.encrypt(generateCouponParams.toJSONString()));
        
        JSONObject couponParams = new JSONObject();
        couponParams.put("uri", "/userCoupon/page/receiveCouponIndex");
        couponParams.put("userId", userId);
        couponParams.put("subscribe",jsonUser.get("subscribe"));
        couponParams.put("jsonWechatUser",jsonWechatUser);
        couponParams.put("userCouponEncryptStr", userCouponEncryptStr);
        
        mv.addObject("couponParams", EncryptUtil.encrypt(couponParams.toJSONString()));
        
        
        mv.setViewName("/user/addCarNo");
        mv.addObject("userId", userId);
        return mv;
    }

    /**
     * 绑定车牌
     *
     * @param userReqVo
     * @return
     */
    @ResponseBody
    @RequestMapping("bindCarNo")
    public Map<String, Object> bindCarNo(String carNo,Integer userId) {
        Map<String, Object> resultMap = null;
        try {
            LogUtils.debug(carNo+userId);
            UserReqVo userReqVo=new UserReqVo();
            userReqVo.setCarNo(carNo);
            userReqVo.setId(userId);
            resultMap = userInfoService.bindCarNoGZ(userId, carNo);
            if(StringUtils.isBank((String)resultMap.get("error"))){
                return ResultMapUtil.genDefaultSuccessMap();
            }else{
                return ResultMapUtil.genErrorMap((String)resultMap.get("error"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMapUtil.genErrorMap("该车牌已开无感支付,请手动删除，如需找回请联系公众号客服");
        }
    }

   
}
